<?php
ob_start();
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include '../operations/connection.php';

if(!isset($_SESSION['user']))
{
	header( 'Location: login.php' );
}
else
{
	$username = $_SESSION['fullname'];
	$id_employee = $_SESSION['IDEmployee'];
}

$a= (string)(microtime(true)*1000);
$b=split('\.',$a);
$idtestproject= $b[0];
$idproject = $_REQUEST['id'];
$page = isset ( $_REQUEST["page"] ) ? intval ( $_REQUEST["page"] ) : 1;
$tab = isset ( $_REQUEST["tab"] ) ? intval ( $_REQUEST["tab"] ) : 1;
$titleerror = $_REQUEST['titleerror'].'-%$#@!-#'.$_REQUEST['titleerrorhidden'];
$titleerror =  mysql_real_escape_string ($titleerror);
$contenterror = $_REQUEST['ncontent'];
$contenterror = mysql_real_escape_string ($contenterror);
//$timeDate=date("Y-m-d H:i:s");
$status = 0;
$delete = 0;
$idtestpro = $_REQUEST['idtestproject'];

if(isset($_REQUEST['addnewerror'])){
	$sql ="INSERT INTO pr_testproject(IDTest,IDProject,IDEmployee,Title,TextTest,Time,`Status`,`Delete`)
	 VALUES('$idtestproject','$idproject','$id_employee','$titleerror','$contenterror',NOW(),'$status','$delete')";
	mysql_query($sql);
	//insert attach
	if (isset($_REQUEST['attachpath'])) {
		$sqlattachs = "insert into pre_attach(testprojectid, filepath, filereal, filename, filetype, filesize, createdate) values";
		foreach ($_REQUEST['attachpath'] as $idx => $valpath){
			if ($_REQUEST['flagattach'][$idx] == 'true') {//insert database
				$sqlattachs = $sqlattachs."('".$idtestproject."','".$_REQUEST['attachpath'][$idx]."','".$_REQUEST['attachreal'][$idx]."','".$_REQUEST['attachfile'][$idx]."','".$_REQUEST['attachtype'][$idx]."','".$_REQUEST['attachsize'][$idx]."',NOW()),";
			}else{//delete file attach
				unlink('../'.$_REQUEST['attachpath'][$idx].$_REQUEST['attachreal'][$idx]);
			}
		}
		$sqlattachs = substr($sqlattachs,0,-1);
		mysql_query($sqlattachs);
	}
	$tab = 1;

}else if($_REQUEST['update']){
	$sql ="UPDATE pr_testproject SET Title = '$titleerror', TextTest = '$contenterror' WHERE IDTest = '$idtestpro'";
	mysql_query($sql);
	//update attach
	if (isset($_REQUEST['attachpath'])) {
		$sqlattachs = 'insert into pre_attach(testprojectid, filepath, filereal, filename, filetype, filesize, createdate) values';
		$sqlattachsremove = 'DELETE from pre_attach where ';
		foreach ($_REQUEST['attachpath'] as $idx => $valpath){
			if ($_REQUEST['attachid'][$idx]=='-1') {//the attach out of the database
				if ($_REQUEST['flagattach'][$idx] == 'true') {//insert database
					$sqlattachs = $sqlattachs."('".$idtestpro."','".$_REQUEST['attachpath'][$idx]."','".$_REQUEST['attachreal'][$idx]."','".$_REQUEST['attachfile'][$idx]."','".$_REQUEST['attachtype'][$idx]."','".$_REQUEST['attachsize'][$idx]."',NOW()),";
				}
			}else {//the attach into the database
				if ($_REQUEST['flagattach'][$idx] == 'false') { //remove database
					$sqlattachsremove = $sqlattachsremove." id=".$_REQUEST['attachid'][$idx]." or";
					//delete file attach
					unlink('../'.$_REQUEST['attachpath'][$idx].$_REQUEST['attachreal'][$idx]);
				}
			}
		}
		$sqlattachs = substr($sqlattachs,0,-1);
		mysql_query($sqlattachs);
		
		$sqlattachsremove = substr($sqlattachsremove,0,-2);
		mysql_query($sqlattachsremove);
	}
}
?>
<?php
mysql_close($connection);


if(isset($_REQUEST['addnewerror'])||isset($_REQUEST['update'])){
	header("Location: ../project.php?projectDetail=project-detail&id=$idproject&tab=$tab&page=$page");
}
ob_flush();
?>
